<template>
    <div class="wrapper">
        <HeaderNav title="order"></HeaderNav>
        <AddressItem></AddressItem>
        <div class="orders_wrapper">
            <OrderItem v-for="item in shopcartItems" :item="item"></OrderItem>
        </div>
        <div class="bottom">
            <div class="pay_item_wrapper">
                <div class="pay_item">
                    <div class="pay_check">
                        <input type="radio" name="pay" value="支付宝"
                               :checked="index_checked=='支付宝'"
                               @change="changeChecked('支付宝')"
                        ></input>
                        <div class="text"> 支付宝</div>
                    </div>
                </div>
                <div class="pay_item">
                    <div class="pay_check">
                        <input type="radio" name="pay" value="微信"
                               :checked="index_checked=='微信'"
                               @change="changeChecked('微信')"
                        ></input>
                        <div class="text"> 微信</div>
                    </div>
                </div>
                <div class="pay_item">
                    <div class="pay_check">
                        <input type="radio" name="pay" value="定易"
                               :checked="index_checked=='定易'"
                               @change="changeChecked('定易')"
                        ></input>
                        <div class="text"> 定易</div>
                    </div>
                </div>
            </div>

            <div class="price_buy_order">
                <div class="label">总计：
                    <strong>￥{{totalPrice}}</strong>
                </div>
                <div class="wrapp_buy_it_button">
                    <div class="buy_it_button" @click="mackOrder"> 立即购买</div>
                </div>
            </div>

        </div>
    </div>
</template>
<script>
    import HeaderNav from '../../components/HeaderNav'
    import OrderItem from './orderItem'
    import {getOrder} from "./service";
    import withStore from "../../vuex/withStore";
    import {getLocal, setLocal} from "../../utils/local";
    import {make_order} from "../shopcart/service";
    import AddressItem from './addressItem'
    import myalert from "../../utils/myalert";
    import {get_shop_cart_items} from "../product/shopcartModal/service";
    import Auth from "../../components/auth";

    export default Auth(withStore({
        components: {
            HeaderNav, OrderItem, AddressItem
        },
        computed: {
            shopcartItems() {
                var shopcartItems = this.$store.state.shopcart.select_array
                if (shopcartItems.length == 0) shopcartItems = getLocal("shopcart.select_array")
                return shopcartItems
            },
            totalPrice() {
                var price = 0;
                this.shopcartItems.map(item => {
                    price += item.unitPrice * item.quantity
                })
                return price
            }
        },
        mounted() {
            var self = this
        },
        data() {
            return {
                index_checked: '定易'
            }
        },
        methods: {
            changeChecked(item) {
                this.index_checked = item
            },
            mackOrder() {
                var self = this
                if (this.index_checked != '定易') {
                    myalert.fail("不支持此类型支付")
                    return false
                }
                myalert.confirm("确定生成订单").then(res => {
                    myalert.loading()
                    make_order(self.$store.state.shopcart.select_array).then(({orders, order}) => {
                        myalert.clear()
                        self.setStore("shopcart.select_array", [])
                        setLocal("shopcart.select_array", [])
                        self.setStore("order.orders", orders)
                        self.setStore("order.order", order)
                        //刷新shopcart
                        get_shop_cart_items().then(shopcartItems => {
                            self.setStore("shopcart.shopcartItems", shopcartItems)
                            self.$router.replace(`/order/${order.objectId}`)
                        })

                    })
                })

            }
        }
    }))
</script>
<style lang="less" scoped>
    .wrapper {
        .bottom {
            padding-left: px2rem(20);
            padding-right: px2rem(20);
            padding-top: px2rem(20);
            padding-bottom: px2rem(20);
            position: fixed;
            bottom: 0;
            left: 0;
            max-width: 750px;
            width: 100%;
            border-top: 1px solid #eee;
            .pay_item_wrapper {
                display: flex;
                .pay_item {
                    margin-right: 10px;
                }
                .pay_check {
                    display: flex;
                }
                margin-bottom: 5px;
            }

            .price_buy_order {

                display: flex;
                align-items: center;
                .label {
                    flex: 1;
                }
                .wrapp_buy_it_button {
                    .buy_it_button {
                        color: #fff;
                        cursor: pointer;
                        text-align: center;
                        display: flex;
                        justify-content: center;
                        align-self: center;
                        align-items: center;
                        width: 2rem;
                        height: .36rem;
                        border-radius: 20px;
                        background-image: linear-gradient(90deg, #ff4e00, red);
                    }
                }
            }
        }
    }
</style>